package com.monitor.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.BlankUtil;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 需求汇总表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-11
 */
@RestController
@RequestMapping("/erpOrderd")
@Api(description="需求汇总")
public class ErpOrderdController {

    @Resource
    private ErpOrderdService orderdService;
    @Resource
    private ErpOrderdMaterielService orderdMaterielService;
    @Resource
    private ErpCustomerService customerService;

    @Resource
    private  ErpOrderdService erpOrderdService;
    @Resource
    private  ErpClientsOrderdService erpClientsOrderdService;


    @Resource
    private ErpClientsMaterielService erpClientsMaterielService;

    @Resource
    private  ErpOrderdMaterielService erpOrderdMaterielService;

    @Resource
    private  ErpWarehouseService erpWarehouseService;

    /*@RequestMapping("/inertOrUpdateOrderd")
    @ApiOperation("添加Or修改需求")
    @ApiImplicitParams({
           *//* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*//*
    })
    public @ResponseBody
    Result<ErpOrderd> inertOrUpdateOrderd(ErpOrderd orderd, List<ErpOrderdMateriel> orderdMateriels) {
        Result<ErpOrderd>  result=new Result<ErpOrderd>();
        try{
            orderdService.insertOrUpdate(orderd);
            for (ErpOrderdMateriel o:orderdMateriels) {
                o.setOrderId(orderd.getId());
                orderdMaterielService.insertOrUpdate(o);
            }
            orderd.setOrderdMateriels(orderdMateriels);
            result.setCount(1);
            result.setData(orderd);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }*/

    @RequestMapping("/inertOrUpdateOrderd")
    @ApiOperation("添加Or修改需求.zhu注：一个需求只有一个需求物料")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "i required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpOrderd> inertOrUpdateOrderd(ErpOrderd orderd, ErpOrderdMateriel orderdMateriels,Integer materield) {
        Result<ErpOrderd>  result=new Result<ErpOrderd>();
        try{
            orderd.setSheetId(DataUtil.getStringDate());
            orderd.setDt(DataUtil.getStringDateShort());
            orderdService.insertOrUpdate(orderd);
            //for (ErpOrderdMateriel o:orderdMateriels) {
            orderdMateriels.setId(materield);
            orderdMateriels.setOrderId(orderd.getId());
            orderdMaterielService.insertOrUpdate(orderdMateriels);
            //}
            orderd.setOrderdMateriels(orderdMateriels);
            result.setCount(1);
            result.setData(orderd);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getOrderdById")
    @ApiOperation("根据需求id获取需求汇总信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "orderId", value = "需求ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpOrderd> getOrderdById(Integer orderId) {
        Result<ErpOrderd> result=new Result<ErpOrderd>();
        try{
            ErpOrderd orderd=orderdService.selectById(orderId);


            ErpOrderdMateriel orderdMateriels=orderdMaterielService.selectOne(new EntityWrapper<ErpOrderdMateriel>().eq("order_id",orderId));
            if (null!=orderdMateriels){
                orderd.setOrderdMateriels(orderdMateriels);
                result.setCount(1);
                result.setData(orderd);
            }

        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeOrderdById")
    @ApiOperation("根据需求id删除")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "orderId", value = "需求ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeOrderdById(Integer orderId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(orderdService.deleteById(orderId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getOrderdPage")
    @ApiOperation("根据客户ID查询需求分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpOrderd>> getOrderdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                              @RequestParam(value="limit",defaultValue="10") int limit, Integer custId, @RequestParam(value="startDate",defaultValue="") String startDate,@RequestParam(value="endDate",defaultValue="")String endDate ) {
        Result<List<ErpOrderd>> result=new Result<List<ErpOrderd>>();
        try{

            List<ErpOrderd> carPage=orderdService.selectList(new EntityWrapper<ErpOrderd>().eq("customer_id",custId).between("dt",startDate,endDate));
            for (int i = 0; i < carPage.size(); i++) {
                ErpOrderd erpOrderd =  carPage.get(i);
                ErpOrderdMateriel orderdMateriel=orderdMaterielService.selectOne(new EntityWrapper<ErpOrderdMateriel>().eq("order_id",erpOrderd.getId()));
                erpOrderd.setOrderdMateriels(orderdMateriel);
            }


            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getOrderdMaterielPage")
    @ApiOperation("根据客户ID查询需求分页-品种")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpOrderd>> getOrderdMaterielPage( Integer custId,String startDate,String endDate) {
        Result<List<ErpOrderd>> result=new Result<>();
        try{
            List<Map<String,String>> mapList=orderdMaterielService.getMaterielGroup();
            List<Integer> orderids=new ArrayList<>();
            mapList.stream().forEach(map->{
                List<Integer> orderdIds=orderdService.getOrderdId(map.get("materiel_name"),map.get("materiel_brand"),map.get("materiel_Specifications"),map.get("materiel_model"));
                if(orderdIds.size()>0){
                    orderids.add(orderdIds.get(0));
                }
            });
            String orders=BlankUtil.listToString(orderids);

            List<ErpOrderd> carPage=null;
            if(StringUtils.isEmpty(startDate)){
                carPage=orderdService.selectList(new EntityWrapper<ErpOrderd>().eq("customer_id",custId).in("id",orders));
            }else{
                carPage=orderdService.selectList(new EntityWrapper<ErpOrderd>().eq("customer_id",custId).and().ge("dt",startDate).and().le("dt",endDate).in("id",orders));
            }
//            for (int i = 0; i < carPage.size(); i++) {
//                ErpOrderd erpOrderd =  carPage.get(i);
//                ErpOrderdMateriel sheet_id = orderdMaterielService.selectOne(new EntityWrapper<ErpOrderdMateriel>().eq("sheet_id", erpOrderd.getSheetId()));
//                        erpOrderd.setOrderdMateriels(sheet_id);
//
//            }

            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    @RequestMapping("/getListOrderds")
    @ApiOperation("品种汇总")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpClientsMateriel>> getListOrderds( Integer custId,String startDate,String endDate){
        Result<List<ErpClientsMateriel>> result =new Result<>();

        try{
           List<ErpClientsMateriel> list=new ArrayList<>();
            List<ErpOrderd> customer_id = erpOrderdService.selectList(new EntityWrapper<ErpOrderd>().eq("customer_id", custId).between("dt",startDate,endDate));
            customer_id.parallelStream().forEach(erpOrderd ->
                    {
                        ErpClientsOrderd orderd_id = erpClientsOrderdService.selectOne(new EntityWrapper<ErpClientsOrderd>().eq("orderd_id", erpOrderd.getId()));
                      if (orderd_id!=null){

                        ErpClientsMateriel erpClientsMateriel = erpClientsMaterielService.selectOne(new EntityWrapper<ErpClientsMateriel>().eq("clients_id", orderd_id.getClientsId()));
                        ErpOrderdMateriel order_id = erpOrderdMaterielService.selectOne(new EntityWrapper<ErpOrderdMateriel>().eq("order_id", orderd_id.getOrderdId()));
                            if(erpClientsMateriel!=null&&order_id!=null){
                                ErpWarehouse erpWarehouse = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("materiel_name", erpClientsMateriel.getMaterielName()).eq("materiel_Specifications", erpClientsMateriel.getMaterielSpecifications())
                                        .eq("materiel_model", erpClientsMateriel.getMaterielModel()).eq("materiel_place", erpClientsMateriel.getMaterielPlace()).eq("materiel_state", erpClientsMateriel.getMaterielState()).eq("materiel_brand", erpClientsMateriel.getMaterielBrand())
                                );
                                    if (erpWarehouse!=null){
                                        erpClientsMateriel.setJingNum(erpWarehouse.getMaterielWarnAmount());
                                    }else {
                                        erpClientsMateriel.setJingNum(0);
                                    }
                                erpClientsMateriel.setXuNum(order_id.getMaterielAmount());
                                list.add(erpClientsMateriel);
                            }
                    }
                    }

            );
//            erpClientsOrderdService
            result.setData(list);
    }catch (Exception exception){
        exception.printStackTrace();
    }
    return  result;
}

    @RequestMapping("/selectByType")
    @ApiOperation("品种汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public Result<List<Map<String, Object>>> selectByType(@RequestParam(value="startDate",defaultValue="")String startDate,@RequestParam(value="endDate",defaultValue="")String endDate,int custId){
    	return orderdService.selectByType(startDate,endDate,custId);
    }
    
    @RequestMapping("/selectByCustId")
    @ApiOperation("品种汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public Result<List<Map<String, Object>>> selectByCustId(int custId){
    	return orderdService.selectByCustId(custId);
    }
    
    @RequestMapping("/getListOrderd")
    @ApiOperation("根据客户ID查询需求")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<Map<String, Object>>> getListOrderd(@RequestParam(value="startDate",defaultValue="")String startDate,@RequestParam(value="endDate",defaultValue="")String endDate,@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
       /* Result<List<ErpOrderd>> result=new Result<List<ErpOrderd>>();
        try{
            List<ErpOrderd> carPage=orderdService.selectList(new EntityWrapper<ErpOrderd>().eq("customer_id",custId).and().ge("dt",startDate).and().le("dt",endDate).orderBy("dt", false));
            List<ErpOrderd> removeList=new ArrayList<>();
            for (ErpOrderd orderd:carPage) {
                List<ErpOrderdMateriel> matList=orderdMaterielService.selectList(new EntityWrapper<ErpOrderdMateriel>().eq("order_id",orderd.getId()));
                if(matList==null||matList.size()<=0) {
                	removeList.add(orderd);
                }else {
                	orderd.setMatList(matList);
                }
            }
            carPage.removeAll(removeList);
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}*/
        return erpOrderdService.selectByOrderList(startDate, endDate, custId);
    }

    @RequestMapping("/getOrderdPageBycustId")
    @ApiOperation("根据客户ID查询需求分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpOrderd>> getOrderdPageBycustId(@RequestParam(value = "page", defaultValue = "1") int page,
                                          @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<Page<ErpOrderd>> result=new Result<Page<ErpOrderd>>();
        try{
            Page<ErpOrderd> page1=new Page<ErpOrderd>(page,limit);
            String custIdsStd=customerService.getcustOfMe(custId);
            Page<ErpOrderd> carPage=orderdService.selectPage(page1,new EntityWrapper<ErpOrderd>().in("customer_id",custIdsStd));
            for (ErpOrderd orderd:carPage.getRecords()) {
                ErpOrderdMateriel orderdMateriel=orderdMaterielService.selectOne(new EntityWrapper<ErpOrderdMateriel>().eq("order_id",orderd.getId()));
                orderd.setOrderdMateriels(orderdMateriel);
            }
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/selectSheetId")
    @ApiOperation("获取需求计划订单号")
    @ApiImplicitParam(name = "time", value = "时间", required = false, dataType = "String", paramType = "query")
    public Result<String> selectSheetId(String time){
    	time=time.substring(0, 10);
    	return orderdService.selectSheetId(time);
    }
    
    @RequestMapping("/addOrderd")
    @ApiOperation("添加或修改需求计划")
    public Result<Integer> addOrderd(ErpOrderd ord,String json){
    	return orderdService.addOrderd(ord, json);
    }
    
    @RequestMapping("/selectOrderByTime")
    @ApiOperation("按时间查询需求计划")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query")
    })
    public Result<List<Map<String, Object>>> selectOrderByTime(String startTime,String endTime,int custId){
    	return orderdService.selectOrderByTime(startTime, endTime,custId);
    }
    
    @RequestMapping("/deleteOrder")
    @ApiOperation("需求计划删除")
    @ApiImplicitParam(name = "orderId", value = "需求计划id", required = false, dataType = "int", paramType = "query")
    public Result<Integer> deleteOrder(int orderId){
    	return orderdService.deleteOrder(orderId);
    }
    
    @RequestMapping("/selectByOrderId")
    @ApiOperation("需求计划详情")
    @ApiImplicitParam(name = "orderId", value = "需求计划id", required = false, dataType = "int", paramType = "query")
    public Result<Map<String, Object>> selectByOrderId(int orderId){
    	return orderdService.selectByOrderId(orderId);
    }



    @RequestMapping("/getListOrderd_lxl")
    @ApiOperation("根据客户ID查询需求")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpOrderd>> getListOrderd_lxl(@RequestParam(value = "page", defaultValue = "1") int page,
                                          @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<List<ErpOrderd>> result=new Result<List<ErpOrderd>>();
        try{
            List<ErpOrderd> page1=new ArrayList<ErpOrderd>();
            List<ErpOrderd> carPage=orderdService.selectList(new EntityWrapper<ErpOrderd>().eq("customer_id",custId));
            for (ErpOrderd orderd:carPage) {
                ErpOrderdMateriel orderdMateriel=orderdMaterielService.selectOne(new EntityWrapper<ErpOrderdMateriel>().eq("order_id",orderd.getId()));
                orderd.setOrderdMateriels(orderdMateriel);
            }
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
}

